//
//  HttpRequestManager.m
//  BossSystem
//
//  Created by hzyd-wlj on 14-7-30.
//  Copyright (c) 2014年 hzyd. All rights reserved.
//

#import "HttpRequestManager.h"
#import "SVHTTPClient.h"
#import "SVProgressHUD.h"
#import "Define.h"
#import "KInterFace.h"
#import "JSONKit.h"
NSString *const HttpRequestManagerDidGetNeedReloginNotificationKey = @"HttpRequestManagerDidGetNeedReloginNotificationKey";

@implementation HttpRequestManager
/**
 *  企信服务器basePath：KWebBasePath为这个
 *
 *  @param methodName
 *  @param parameters
 *  @param completedMainBlock
 */
+ (void)postMethod:(NSString *)methodName parameters:(NSDictionary *)parameters completedMainBlock:(completedMainBlock)completedMainBlock
{
    //TODO:优化请求错误的处理
    [[SVHTTPClient sharedClient] POST:methodName parameters:parameters completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *errMsg;
        //FIXME:
        errMsg = [self isCurrentLanguageEnglish]?@"英文错误信息":@"中文错误信息";
        if (!error) {
            if ([response[@"code"] intValue] == 0) {
                completedMainBlock(YES,response,errMsg);
            }else{
                NSString *path = [[NSBundle mainBundle]pathForResource:@"codeMessage" ofType:@"txt"];
                NSString *data = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
                NSArray *codeArray = [data componentsSeparatedByString:@","];
                NSMutableDictionary *codeDict = [[NSMutableDictionary alloc]init];
                [codeArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
                    [codeDict setValue:[obj componentsSeparatedByString:@":"][1] forKey:[obj componentsSeparatedByString:@":"][0]];
                }];
                NSString *codeKey = [NSString stringWithFormat:@"%@",response[@"code"]];
                completedMainBlock(NO,response,[[codeDict copy]objectForKey:codeKey]);
            }
        }else{
            //网络方面的错误
            errMsg = [NSString stringWithFormat:@"网络错误或服务器未响应!（%ld:%@）",(long)error.code,[error localizedDescription]];
            [SVProgressHUD showErrorWithStatus:errMsg];
            completedMainBlock(NO,nil,errMsg);
            DLog(@"网络请求错误，服务器未响应%@",errMsg);
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            DLog(@"-----%@-----",str);
        }
    }];
}
/**
 *  get请求，企信服务器basePath：KQXWebBasePath为这个
 *
 *  @param methodName
 *  @param parameters
 *  @param completedMainBlock
 */
+ (void)getMethod:(NSString *)methodName parameters:(NSDictionary *)parameters completedMainBlock:(completedMainBlock)completedMainBlock
{
    [[SVHTTPClient sharedClient] GET:methodName parameters:parameters completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *errMsg;
        //FIXME:
        errMsg = [self isCurrentLanguageEnglish]?@"英文错误信息":@"中文错误信息";

        if (!error) {
            if ([response[@"code"] intValue] == 0) {
                completedMainBlock(YES,response,errMsg);
            }else{
                NSString *path = [[NSBundle mainBundle]pathForResource:@"codeMessage" ofType:@"txt"];
                NSString *data = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
                NSArray *codeArray = [data componentsSeparatedByString:@","];
                NSMutableDictionary *codeDict = [[NSMutableDictionary alloc]init];
                [codeArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
                    [codeDict setValue:[obj componentsSeparatedByString:@":"][1] forKey:[obj componentsSeparatedByString:@":"][0]];
                }];
                NSString *codeKey = [NSString stringWithFormat:@"%@",response[@"code"]];
                completedMainBlock(NO,response,[[codeDict copy]objectForKey:codeKey]);
            }
        }else{
            //网络方面的错误
            errMsg = [NSString stringWithFormat:@"网络错误或服务器未响应!（%ld:%@）",(long)[error code],[error localizedDescription]];
            [SVProgressHUD showErrorWithStatus:errMsg];
            completedMainBlock(NO,nil,errMsg);
            DLog(@"网络请求错误%@",errMsg);
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            DLog(@"-----%@-----",str);
        }
    }];
}

+ (void)putMethod:(NSString *)methodName parameters:(NSDictionary *)parameters completedMainBlock:(completedMainBlock)completedMainBlock
{
    //TODO:优化请求错误的处理
    [[SVHTTPClient sharedClient] PUT:methodName parameters:parameters completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *errMsg;
        //FIXME:
        errMsg = [self isCurrentLanguageEnglish]?@"英文错误信息":@"中文错误信息";
        if (!error) {
            if ([response[@"code"] intValue] == 0) {
                completedMainBlock(YES,response,errMsg);
            }else{
                NSString *path = [[NSBundle mainBundle]pathForResource:@"codeMessage" ofType:@"txt"];
                NSString *data = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
                NSArray *codeArray = [data componentsSeparatedByString:@","];
                NSMutableDictionary *codeDict = [[NSMutableDictionary alloc]init];
                [codeArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
                    [codeDict setValue:[obj componentsSeparatedByString:@":"][1] forKey:[obj componentsSeparatedByString:@":"][0]];
                }];
                NSString *codeKey = [NSString stringWithFormat:@"%@",response[@"code"]];
                completedMainBlock(NO,response,[[codeDict copy]objectForKey:codeKey]);
            }
        }else{
            //网络方面的错误
            errMsg = [NSString stringWithFormat:@"网络错误或服务器未响应!（%ld:%@）",(long)error.code,[error localizedDescription]];
            [SVProgressHUD showErrorWithStatus:errMsg];
            completedMainBlock(NO,nil,errMsg);
            DLog(@"网络请求错误，服务器未响应%@",errMsg);
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            DLog(@"-----%@-----",str);
        }
    }];
}

+ (void)deleteMethod:(NSString *)methodName parameters:(NSDictionary *)parameters completedMainBlock:(completedMainBlock)completedMainBlock
{
    [[SVHTTPClient sharedClient] DELETE:methodName parameters:parameters completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *errMsg;
        //FIXME:
        errMsg = [self isCurrentLanguageEnglish]?@"英文错误信息":@"中文错误信息";
        if (!error) {
            if ([response[@"code"] intValue] == 0) {
                completedMainBlock(YES,response,errMsg);
            }else{
                NSString *path = [[NSBundle mainBundle]pathForResource:@"codeMessage" ofType:@"txt"];
                NSString *data = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
                NSArray *codeArray = [data componentsSeparatedByString:@","];
                NSMutableDictionary *codeDict = [[NSMutableDictionary alloc]init];
                [codeArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
                    [codeDict setValue:[obj componentsSeparatedByString:@":"][1] forKey:[obj componentsSeparatedByString:@":"][0]];
                }];
                NSString *codeKey = [NSString stringWithFormat:@"%@",response[@"code"]];
                completedMainBlock(NO,response,[[codeDict copy]objectForKey:codeKey]);
            }
        }else{
            //网络方面的错误
            errMsg = [NSString stringWithFormat:@"网络错误或服务器未响应!（%ld:%@）",(long)[error code],[error localizedDescription]];
            [SVProgressHUD showErrorWithStatus:errMsg];
            completedMainBlock(NO,nil,errMsg);
            DLog(@"网络请求错误%@",errMsg);
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            DLog(@"-----%@-----",str);
        }
    }];
}

+ (BOOL)isCurrentLanguageEnglish
{
    NSArray *languages = [NSLocale preferredLanguages];
    return [[languages objectAtIndex:0] isEqualToString:@"en"];
}
@end
